/*
 * main.c
 *
 *  Created on: 2018年10月16日
 *  Author: Degang.Wu (windragon@126.com)
 *  Desc:
 */

#include <stdio.h>
#include "public.h"
#include "enc_common.h"
#include "../rtc/hi_rtc.h"

//local variable
#ifdef __SLAVE
static int 		g_procpid = -1;
#endif

void usage()
{
	printf("usage: %s [-monitor] [-idx chlidx] [-dump index] [-cfgpath configfile_path]\n",
			u::Util::module_name());
	printf("example for hienc_monitor_master : BWHIENCAPP -master -monitor -cfgpath %s\n", ENC_CFGPATH);
	printf("example for hienc_monitor_slave : BWHIENCAPP -slave -monitor -cfgpath %s\n", ENC_CFGPATH);
	printf("example for audio demux : BWHIENCAPP -ademux\n");
	printf("example for hienc_app : BWHIENCAPP -idx 0 \n");
	printf("example for hienc_dump : BWHIENCAPP -master -dump [0] \n");
	printf("params:\n");
	printf("\t -master\tset master mode\n");
	printf("\t -slave\t\tset slave mode(default mode)\n");
	printf("\t -cfgpath\tset config path(default:/bwsc/hienc/cfg/)\n");
	printf("\t -dump [idx]\tdump share memory\n");
	printf("\t -mstip\t\tset master ip addr(default is 10.10.10.10)\n");
	printf("\t -slvip\t\tset first sub board's ip addr(default begin from 10.10.10.100)\n");
	printf("\t -idx\t\tstart encoder app for idx channel\n");
	printf("\t -version\tget version\n");
	printf("\t -gettime\tget rtc time\n");
}

static int init(int argc, char ** argv)
{
	char dir_cmd[SZ_1K];
	int rtval = 0;

	snprintf(g_cfgpath, sizeof(g_cfgpath), "%s", ENC_CFGPATH);

	hienc_set_mstip(ENC_DFLT_MST_IP);
	hienc_set_slv0ip(ENC_DFLT_SLV0_IP);

	for (int i = 1; i < argc; i++)
	{
		if (strcmp("-master", argv[i]) == 0)
		{
			enc_set_master(1);
		}
		else if (strcmp("-slave", argv[i]) == 0)
		{
			enc_set_master(0);
		}
		else if (strcmp("-cfgpath", argv[i]) == 0 && (i+1) < argc)
		{
			snprintf(g_cfgpath, sizeof(g_cfgpath), "%s", argv[i + 1]);
		}
		else if (strcmp("-mstip", argv[i]) == 0 && (i+1) < argc)
		{
			if (hienc_set_mstip(argv[i+1]) < 0)
			{
				printf("master ip[%s] is invalid\n", argv[i+1]);
				return -1;
			}
		}
		else if (strcmp("-slvip", argv[i]) == 0 && (i+1) < argc)
		{
			if (hienc_set_slv0ip(argv[i+1]) < 0)
			{
				printf("slave ip[%s] is invalid\n", argv[i+1]);
				return -1;
			}
		}
	}

	rtval = strlen(g_cfgpath);
	if (rtval > 0 && g_cfgpath[rtval - 1] != '\\' && g_cfgpath[rtval - 1] != '/')
	{
		strcat(g_cfgpath, "/");
	}

	if (!u::io::File::exists(g_cfgpath))
	{
		//make sure cfgpath is ok!
		snprintf(dir_cmd, sizeof(dir_cmd), "mkdir -p %s", g_cfgpath);
		rtval = system(dir_cmd);
		if (rtval < 0)
		{
			BW_INFO("system failed\n");
		}
	}

	rtval = chdir(u::Util::module_folder());	// init pwd,
	if (rtval < 0)
	{
		BW_INFO("chdir failed\n");
	}

	return 0;
}

void debug_dummy_data(void)
{
	pENC_CHLSET p_chanSet;

	p_chanSet = &g_encset_slv->enc_chls[g_chlidx];

	p_chanSet->encchl_isrun = 1;
	p_chanSet->encchl_vid.vid_codec = 1;
	p_chanSet->encchl_vid.vid_width = 1920;
	p_chanSet->encchl_vid.vid_height = 1080;
	p_chanSet->encchl_vid.vid_fpsnum = 1;
	p_chanSet->encchl_vid.vid_fpsden = 60;
	p_chanSet->encchl_vid.vid_bitrate_kb = 4000;
	p_chanSet->encchl_vid.vid_rctype = 0; //CBR
	p_chanSet->encchl_vid.vid_profile = 3;
	p_chanSet->encchl_vid.vid_gopsize = 60;

	p_chanSet->encchl_ts.ts_pcr_interval_ms = 30;
	p_chanSet->encchl_ts.ts_pcr_pid = 1605 + g_chlidx;
	p_chanSet->encchl_ts.ts_service_id = 1331 + g_chlidx;
	p_chanSet->encchl_ts.ts_pmt_pid = 1043 + g_chlidx;
	p_chanSet->encchl_ts.ts_aud_pid = 1085 + g_chlidx;
	p_chanSet->encchl_ts.ts_vid_pid = 1075 + g_chlidx;
	p_chanSet->encchl_ts.ts_muxrate_kb = p_chanSet->encchl_vid.vid_bitrate_kb * 110 / 100;
	sprintf((char*)p_chanSet->encchl_ts.ts_service_name, "CCTV %d", g_chlidx);
	sprintf((char*)p_chanSet->encchl_ts.ts_provider_name, "CCTV sub %d", g_chlidx);

	p_chanSet->encchl_output.ethio_dipaddr = 0xC0A864B9; //192.168.100.185
	//p_chanSet->encchl_output.ethio_dipaddr = 0xC0A864FF; //192.168.100.255
	p_chanSet->encchl_output.ethio_dipport = 60000 + g_chlidx;	
}

int main(int argc, char **argv)
{	
	init(argc, argv);

	for (int i = 1; i < argc; i++)
	{
		if (strcmp("-monitor", argv[i]) == 0)
		{
			return hienc_monitor(argc, argv);
		}
		else if (strcmp("-dump", argv[i]) == 0)
		{
			if (i+1 < argc)
			{
				g_dump_chlidx = atoi(argv[i+1]);
			}
			return shrmem_dump(argc, argv);
		}
        else if (strcmp("-version", argv[i]) == 0)
        {
	        fprintf(stdout, "%s", hienc_get_version());
//            fprintf(stdout, "v2.0.0");
            return 0;
        }
		else if (strcmp("-idx", argv[i]) == 0 && (i+1) < argc)
		{
			g_chlidx = atoi(argv[i+1]);
			// start encoder app
		}
		else if (strcmp("-version", argv[i]) == 0)
		{
			printf("%s\n", hienc_get_version());
			return 0;
		}
#ifdef __SLAVE
        else if (strcmp("-gettime", argv[i]) == 0)
        {
        	dump_current_time();
        	return 0;
        }
		else if (strcmp("-ademux", argv[i]) == 0)
		{
			//demux audio input
			return enc_ai_demux();
		}
		else if (strcmp("-testin", argv[i]) == 0)
		{
			return enc_hdmi_input();
		}	
#endif
	}

	if (g_chlidx < 0 || g_chlidx >= ENC_MAX_CHL)
	{
		usage();
		return -1;
	}

	if (shrmem_init(0) == 0)
	{
		return -1;
	}

	// hiapp should run in slave mode
	if (enc_is_master())
	{
		BW_INFO("hiapp must run in slave mode!\n");
		return -1;
	}

#ifdef __SLAVE

	/////////////////////////////////////////////////////
	g_procpid = getpid();
	stENC_CHLSET	*chlset = NULL;
	chlset = &g_encset_slv->enc_chls[g_chlidx];
	if (chlset->encchl_procpid != 0)
	{
		BW_INFO("hisapp open repeated!\n");
		shrmem_uninit();
		return 0;
	}

	chlset->encchl_procpid = g_procpid;
	BW_INFO("ENCAPP[chl=%d,proc=%d]===>runing...!\n", g_chlidx, g_procpid);
	
	uc_set_coredump((char*)"/mnt");

	enc_init(g_chlidx);
	
	while (g_encset_slv->enc_chls[g_chlidx].encchl_isrun) 
	{
		enc_hdmi_inputFormat();
		if ((main_data.hdmi_dev.vi_info.valid) && (main_data.enc_status == 0)) {
			// start hiapp
			if (enc_start(g_encset_slv) < 0) {
				BW_INFO("start video encode error\n");
				return -1;
			}

			if (enc_start_thread() < 0) {
				BW_INFO("start other thread error\n");
				return -1;
			}			
		}
		
		sleep(1);
	}

	enc_stop();
	g_encset_slv->enc_chls[g_chlidx].encchl_procpid = 0;
#endif
	shrmem_uninit();

	return 0;
}
